﻿//+-------------------------------------------------------------------------------------------------------------------------------------------------------
//
//Description: EnvironmentVariableViewModel
//Author: Aravind Ramarathinam (aravr@microsoft.com)
//Copyright © Microsoft Corporation
//Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 
//You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 
//
//THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, 
//INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT. 
//See the Apache Version 2.0 License for specific language governing permissions and limitations under the License.
// 
//--------------------------------------------------------------------------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.ComponentModel;
using VmroleAuthor.Models;

namespace VmroleAuthor.ViewModels
{
    public class EnvironmentVariableViewModel : ViewModelBase, IDataErrorInfo, ICleanupParamsAndPayload
    {     
        private readonly EnvironmentVariable environmentVariable;
        private readonly VmRoleResourceExtensionViewModel vmRoleResourceExtensionViewModel;
        private StringParameterizableProperty envVariableName;
        private StringParameterizableProperty envVariableValue;

        public static class PropertyNames
        {
            public const string Name = "Name";
            public const string Value = "Value";          
        }       

        /// <summary>
        /// Constructor
        /// </summary>
        public EnvironmentVariableViewModel(EnvironmentVariable environmentVariable, VmRoleResourceExtensionViewModel vmRoleResourceExtensionViewModel)
        {
            this.environmentVariable = environmentVariable;
            this.vmRoleResourceExtensionViewModel = vmRoleResourceExtensionViewModel;           
        }

        public EnvironmentVariable EnvironmentVariable
        {
            get { return this.environmentVariable; }            
        }

        public List<string> AvailableParameters
        {
            get { return this.vmRoleResourceExtensionViewModel.ResourceExtensionParametersViewModel.AvailableParameters; }
        }

        public StringParameterizableProperty Name
        {
            get
            {
                if (this.envVariableName == null)
                {
                    Func<string> paramValueGetter = () =>
                        {
                            if (string.IsNullOrEmpty(this.environmentVariable.Name))
                            {
                                return "Enter new environment variable";
                            }
                            return this.environmentVariable.Name;
                        };
                    Action<string> paramValueSetter = delegate(string value) { this.environmentVariable.Name = value; };
                    Func<string> autoParamHintGetter = () => "EnvironmentVariable" + PropertyNames.Name;
                    ViewDefParamInfo viewDefParamInfo = new ViewDefParamInfo
                    {
                        ViewDefParameterType = ViewDefParameterType.String,                                           
                    };

                    this.envVariableName = new StringParameterizableProperty(
                        PropertyNames.Value,
                        paramValueGetter,
                        paramValueSetter,
                        autoParamHintGetter,
                         ParameterType.String,
                        "Environment variable name",
                         this.vmRoleResourceExtensionViewModel.ResourceExtensionParametersViewModel,
                         true,
                         viewDefParamInfo
                        );
                }
                return this.envVariableName;
            }           
        }       

        public StringParameterizableProperty Value
        {
            get
            {
                if (this.envVariableValue == null)
                {
                    Func<string> paramValueGetter = () =>
                        {
                            if (string.IsNullOrEmpty(this.environmentVariable.Value))
                            {
                                return "Enter environment variable value";
                            }
                            return this.environmentVariable.Value;
                        };
                    Action<string> paramValueSetter = delegate(string value) { this.environmentVariable.Value = value; };
                    Func<string> autoParamHintGetter = () =>
                        {
                            if (!string.IsNullOrEmpty(this.environmentVariable.Name) && ParameterizationHelper.IsNotParameterized(this.environmentVariable.Name))
                            {
                                return this.environmentVariable.Name + PropertyNames.Value;
                            }
                            return "EnvironmentVariable" + PropertyNames.Value;
                        };
                    ViewDefParamInfo viewDefParamInfo = new ViewDefParamInfo
                    {
                        ViewDefParameterType = ViewDefParameterType.String,
                    };

                    this.envVariableValue = new StringParameterizableProperty(
                        PropertyNames.Value,
                        paramValueGetter,
                        paramValueSetter,
                        autoParamHintGetter,
                         ParameterType.String,
                        "Environment variable value",
                         this.vmRoleResourceExtensionViewModel.ResourceExtensionParametersViewModel,
                         true,
                         viewDefParamInfo
                        );
                }
                return this.envVariableValue;
            }
        }

        public void Cleanup()
        {
            this.Name.Cleanup();
            this.Value.Cleanup();
        }

        public string this[string propertyName]
        {
            get
            {
                switch (propertyName)
                {
                    case PropertyNames.Name:
                          if (string.IsNullOrEmpty(this.environmentVariable.Name))
                          {
                              return "Environment Variable Name must be specified";
                          }
                        break;
                    case PropertyNames.Value:
                        if (string.IsNullOrEmpty(this.environmentVariable.Value))
                        {
                            return "Environment Variable Value must be specified";
                        }
                        break;
                }
                return null;
            }
        }

        public string Error { get { return null; } }
    }
}
